package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;
import android.text.TextUtils;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.PatrolContract;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.CruiseLocusBean;

import java.util.HashMap;
import java.util.Map;


@ActivityScope
public class PatrolPresenter extends BasePresenter<PatrolContract.Model, PatrolContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public PatrolPresenter(PatrolContract.Model model, PatrolContract.View rootView) {
        super(model, rootView);
    }
    public void  queryCruiseHistory(String adcd,String year,String page,String groupid,boolean isFresh)
    {
        ConfigBean configBean = DataHelper.getDeviceData(mAppManager.getCurrentActivity(), AppConstants.EXTRA_CONFIG_DATA);
        Map<String,String> maps=new HashMap<>();
        maps.put("adcd",configBean.getAdcd());
        maps.put("year",year);
        maps.put("page",page);
        maps.put("size",String.valueOf(Api.PAGE_SIZE_1));
        if (groupid!=null&& !TextUtils.isEmpty(groupid))
        {
            maps.put("groupid",groupid);
        }
        mModel.queryCruiseHistory(DataUtil.getJNUrl(configBean)+"/webmvc/v1/taskhzz/findXcrecordListForApp",maps)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<CruiseLocusBean>(mErrorHandler) {
                    @Override
                    public void onNext(CruiseLocusBean cruiseLocusBean) {
                        if (cruiseLocusBean!=null&&cruiseLocusBean.getCode().equals("0"))
                        {
                            mRootView.queryCruiseHistorySuccess(cruiseLocusBean.getData(),isFresh);
                            return;
                        }else {
                            mRootView.queryCruiseHistorySuccess(null,isFresh);
                        }
                        mRootView.showMessage(cruiseLocusBean.getInfo());
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
